import { z } from "zod"

import { TextPayload } from "./message"
import { UserProfileSchema } from "./user"

export const SessionSchema = {
  conversationID: "",
  toAccount: "",
  type: "C2C",
  subType: "",
  lastMessage: {
    lastTime: 0,
    lastSequence: 0,
    fromAccount: "",
    type: "TIMTextElem",
    payload: {
      text: "",
    },
    cloudCustomData: "",
    isRevoked: false,
    onlineOnlyFlag: false,
    nick: "",
    nameCard: "",
    version: 0,
    isPeerRead: false,
    revoker: "",
    messageForShow: "",
  },
  unreadCount: 0,
  peerReadTime: 0,
  groupAtInfoList: [],
  // groupProfile: {
  //   groupID: "",
  //   name: "",
  //   avatar: "",
  //   type: "Public",
  //   introduction: "",
  //   notification: "",
  //   ownerID: "",
  //   createTime: 1727142895,
  //   infoSequence: 1056,
  //   lastInfoTime: 1756713087,
  //   selfInfo: {
  //     messageRemindType: "AcceptAndNotify",
  //     joinTime: 1742461790,
  //     nameCard: "",
  //     role: "Member",
  //     userID: "admin",
  //     readedSequence: 340,
  //     excludedUnreadSequenceList: [],
  //   },
  //   lastMessage: {
  //     lastTime: 1756712436,
  //     lastSequence: 340,
  //     fromAccount: "zhangal",
  //     type: "TIMTextElem",
  //     payload: {
  //       text: "",
  //     },
  //     cloudCustomData: "",
  //     isRevoked: false,
  //     onlineOnlyFlag: false,
  //     nick: "",
  //     nameCard: "",
  //     revoker: "",
  //     messageForShow: "",
  //   },
  //   nextMessageSeq: 341,
  //   memberCount: 9,
  //   maxMemberCount: 6000,
  //   joinOption: "NeedPermission",
  //   inviteOption: "DisableInvite",
  //   groupCustomField: [],
  //   muteAllMembers: false,
  //   isSupportTopic: false,
  //   muteAllMember: "Off",
  // },
  userProfile: {
    userID: "",
    nick: "",
    gender: "",
    birthday: 0,
    location: "",
    selfSignature: "",
    allowType: "",
    language: 0,
    avatar: "",
    messageSettings: 0,
    adminForbidType: "",
    level: 0,
    role: 0,
    lastUpdatedTime: 0,
    profileCustomField: [],
  },
  remark: "",
  isPinned: false,
  pinned: 0,
  messageRemindType: "",
  markList: [],
  customData: "",
  conversationGroupList: [],
  draftText: "",
  // id: "",
  // createdAt: 0,
  // updatedAt: 0,
}

export const TypeSchema = z.enum(["C2C", "GROUP", "@TIM#SYSTEM"])

export const LastMessageSchema = z
  .object({
    /**
     *  当前会话最新消息的时间戳，单位：秒
     */
    lastTime: z.number().optional(),
    lastSequence: z.number().optional(),
    fromAccount: z.string().optional(),
    type: z.string().optional(),
    payload: TextPayload,
    cloudCustomData: z.string().optional(),
    isRevoked: z.boolean().optional(),
    onlineOnlyFlag: z.boolean().optional(),
    nick: z.string().optional(),
    nameCard: z.string().optional(),
    version: z.number().optional(),
    isPeerRead: z.boolean().optional(),
    revoker: z.string().optional(),
    messageForShow: z.string().optional(),
  })
  .optional()

export const DB_SessionSchema = z.object({
  conversationID: z.string(),
  toAccount: z.string().optional(),
  type: TypeSchema,
  subType: z.string().optional(),
  lastMessage: LastMessageSchema,
  unreadCount: z.number().int().min(0).optional(),
  peerReadTime: z.number().optional(),
  groupAtInfoList: z.array(z.any()).optional(),
  groupProfile: z.any().optional(),
  userProfile: UserProfileSchema,
  remark: z.string().optional(),
  isPinned: z.boolean().optional(),
  messageRemindType: z.string().optional(),
  markList: z.array(z.any()).optional(),
  customData: z.string().optional(),
  conversationGroupList: z.array(z.any()).optional(),
  draftText: z.string().optional(),
  // **************** Custom *************** //
  pinned: z.number().int().min(0).max(1).optional(),
  // **************** Base *************** //
  id: z.string().optional(),
  createdAt: z.number().optional(),
  updatedAt: z.number().optional(),
})

export type DB_Session = z.infer<typeof DB_SessionSchema>
